package com.swgk.core.util;

import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class PointUtils {

    public static String priceAll(String price){
        if (!TextUtils.isEmpty(price)){
            DecimalFormat df = null;
            if (price.indexOf(".") > 0) {//含有小数
                if (price.length() - price.indexOf(".") - 1 == 0) {//含有一位小数
                    df = new DecimalFormat("###,##0.");
                } else if (price.length() - price.indexOf(".") - 1 == 1) {//含有两位小数
                    df = new DecimalFormat("###,##0.0");
                } else {//含有两位以上的小数
                    df = new DecimalFormat("###,##0.00");
                }
            } else {//只有整数部分
                df = new DecimalFormat("###,##0");
            }
            double number = 0.0;
            try {
                number = Double.parseDouble(price);
            } catch (Exception e) {
                number = 0.0;
            }
            return df.format(number);
        }else {
            return "-";
        }
    }

    public static String price(double price){
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        nf.setMinimumFractionDigits(2);
        nf.setMaximumFractionDigits(2);
        return nf.format(price);
    }

    public static String originalPrice(double price){
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        return nf.format(price);
    }

    public static SpannableString setMoney(String s) {
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new AbsoluteSizeSpan(12,true),s.indexOf("."),s.length(),SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

}
